package com.wtwd.campus.adapter;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.wtwd.campus.common.RespCode;
import com.wtwd.campus.entity.VerifyResult;
import com.wtwd.campus.method.DeleteEncrypt;
import com.wtwd.campus.method.PostPutEncrypt;
import com.wtwd.campus.method.QueryEncrypt;
import com.wtwd.campus.service.RedisService;
import com.wtwd.campus.utils.Sha256Utils;
import com.wtwd.campus.utils.Tools;

import javax.servlet.http.HttpServletRequest;
import java.util.TreeMap;

/**
 * @Author ldaoliang
 * @Date create in 9:19 2021/7/16
 * @Description
 */
public class H5Adapter extends VerifyAdapter {

    public H5Adapter(HttpServletRequest request) {
        super(request);
        String method = request.getMethod();
        if(method.equalsIgnoreCase(VerifyAdapter.get)){
            encryptVerify = new QueryEncrypt();
        }else if(method.equalsIgnoreCase(VerifyAdapter.post) || method.equalsIgnoreCase(VerifyAdapter.put)){
            encryptVerify = new PostPutEncrypt();
        }else if(method.equalsIgnoreCase(VerifyAdapter.delete)){
            encryptVerify = new DeleteEncrypt();
        }
    }

    @Override
    public VerifyResult verifyEncrypt(HttpServletRequest request, RedisService redisService) {
        String url = request.getRequestURI();
        if(url.contains("/h5/jumpIndex")){
            VerifyResult verifyResult = new VerifyResult();
            String timeStamp = request.getParameter("timestamp");
            String sign = request.getParameter("sign");
            String appKey = request.getParameter("appKey");
            String userId = request.getParameter("userId");
            if(Tools.stringIsBlank(timeStamp,sign,appKey,userId)){
                verifyResult.setResult(false);
                verifyResult.setRespCode(RespCode.PARAMETERIS_MiSSING);
                return verifyResult;
            }
            TreeMap<String, String> treeMap = new TreeMap();
            treeMap.put("timestamp",timeStamp);
            treeMap.put("appKey",appKey);
            treeMap.put("userId",userId);
            JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(treeMap), Feature.OrderedField);
            if (!sign.equalsIgnoreCase(Sha256Utils.SHA256(json.toJSONString()))) {
                verifyResult.setResult(false);
                verifyResult.setRespCode(RespCode.REQUEST_ILLEGAL);
                return verifyResult;
            }
            return null;
        }else if(url.contains("h5/authenticateAndJump") || url.contains("h5/hnhjyAuthenticateAndJump")
                || url.contains("h5/ryAuthenticationAndJump")||url.contains("h5/chAuthenticationAndJump")||url.contains("h5/reE15AuthenticationAndJump")){
            return null;
        }
        return encryptVerify.verify(sign,accessToken,request,redisService);
    }

    public static void main(String[] args) {
        TreeMap<String, String> treeMap = new TreeMap();
        treeMap.put("timestamp","1692856145000");
        treeMap.put("appKey","zlQekvdBtCTvdOPW");
        treeMap.put("userId","123458");
        JSONObject json = JSONObject.parseObject(JSONObject.toJSONString(treeMap), Feature.OrderedField);
        String s = Sha256Utils.SHA256(json.toJSONString());
        System.out.println(s);
    }
}


